/**
 * 
 */
package travibot.meta;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import travibot.engine.MaterialFactory;
import travibot.meta.dao.MaterialTypeDAO;
import travibot.meta.dao.StructureTypeDAO;
import travibot.meta.dao.UnitTypeDAO;
import travibot.model.Material;
import travibot.utility.DescriptionNotFoundException;

/**
 * @author Petr Macek
 * 
 */
public class MetabaseManager {

    private StructureTypeDAO structureTypeDao;
    private UnitTypeDAO unitTypeDao;
    private MaterialTypeDAO materialTypeDao;

    private MaterialFactory materialFactory;

    private Map<String, StructureType> structureTypes = new HashMap<String, StructureType>();
    private Map<String, UnitType> unitTypes = new HashMap<String, UnitType>();
    private Map<String, MaterialType> materialTypes = new HashMap<String, MaterialType>();

    public StructureTypeDAO getBuildingTypeDao() {

        return structureTypeDao;
    }

    public void setStructureTypeDao(StructureTypeDAO buildingTypeDao) {

        this.structureTypeDao = buildingTypeDao;
    }

    public void setUnitTypeDao(UnitTypeDAO unitTypeDao) {

        this.unitTypeDao = unitTypeDao;
    }

    public void setMaterialTypeDao(MaterialTypeDAO materialTypeDao) {

        this.materialTypeDao = materialTypeDao;
    }

    public void setMaterialFactory(MaterialFactory materialFactory) {

        this.materialFactory = materialFactory;
    }

    public StructureType getStructureType(String title) throws DescriptionNotFoundException {

        StructureType ret = structureTypes.get(title);
        if (ret == null) {
            ret = structureTypeDao.getStructureType(title);
            structureTypes.put(title, ret);
        }
        return ret;
    }

    public void insertStructureType(StructureType type) {

        structureTypeDao.insertStructureType(type);
    }

    public UnitType getUnitType(String name) throws DescriptionNotFoundException {

        UnitType ret = unitTypes.get(name);
        if (ret == null) {

            ret = unitTypeDao.getUnitType(name);
            unitTypes.put(name, ret);
        }

        return ret;
    }

    public void insertUnitType(UnitType type) {

        unitTypeDao.insertUnitType(type);
    }

    public MaterialType getMaterialType(String name) throws DescriptionNotFoundException {

        MaterialType ret = materialTypes.get(name);
        if (ret == null) {

            ret = materialTypeDao.getMaterialType(name);
            materialTypes.put(name, ret);
        }

        return ret;
    }

    public List<MaterialType> getMaterialTypes() {

        List<MaterialType> list = materialTypeDao.getMaterialTypes();
        return list;
    }

    public void insertMaterialType(MaterialType type) {

        materialTypeDao.insertMaterialType(type);
    }

    public MaterialFactory getMaterialFactory() {

        return materialFactory;
    }

    public Material getMaterial(MaterialType type) {

        return getMaterialFactory().getMaterial(type.getImplementingClass());
    }
}
